Macromedia Flex Article

Learning Flex Basics (Part 4): Working with Data Models


Table of Contents

  1. Introduction
  2. Create the Application Object and Application Layout
  3. Add the Form Controls and the DataGrid Controls
  4. Add the New Employee Model, Employee, and Location Models
  5. Instantiate the DataController Class, and Create the Employee and DataController Classes
  6. Add the DataController Methods
  7. Code Listing for data.mxml
  8. Code Listings for DataController.as and Employee.as
  9. Code Listings for locations.xml and employee.xml
  10. Where to Go From Here

Complete Code Listing for data.mxml

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" xmlns="*">
  <mx:Model id="newEmpModel">
    <fName>{fNameInput.text}</fName>
    <lName>{lNameInput.text}</lName>
    <location>{locCombo.selectedItem}</location>
  </mx:Model>
  <mx:Model id="empModel" source="employees.xml"/>  
  <mx:Model id="locModel" source="locations.xml"/>
  <DataController id="dataController"/>
  <mx:HBox>
    <mx:Panel id="empPanel" title="Employee Data" height="{engPanel.height+supPanel.height}">
      <mx:DataGrid id="empGrid" dragEnabled="true">
        <mx:dataProvider>
          {empModel.employee}
        </mx:dataProvider>
        <mx:columns>
          <mx:Array>
            <mx:DataGridColumn columnName="fName" headerText="First Name" />
            <mx:DataGridColumn columnName="lName" headerText="Last Name" />
            <mx:DataGridColumn columnName="location" headerText="Location" />
          </mx:Array>
        </mx:columns>
      </mx:DataGrid>
      <mx:Form>
        <mx:FormHeading label="Add New"/>
        <mx:FormItem label="First Name">
          <mx:TextInput id="fNameInput"/>
        </mx:FormItem>
        <mx:FormItem label="Last Name">
          <mx:TextInput id="lNameInput"/>
        </mx:FormItem>
        <mx:FormItem label="Location">
          <mx:ComboBox id="locCombo" dataProvider="{locModel.location}"/>
        </mx:FormItem>
      </mx:Form>
      <mx:ControlBar>
        <mx:Button label="Add Employee" click="dataController.addNewEmp(newEmpModel,empGrid.dataProvider)"/>
      </mx:ControlBar>
    </mx:Panel>
    <mx:VBox>
      <mx:Panel id="engPanel" title="Engineering Group" width="{empPanel.width}">
        <mx:DataGrid dragEnter="dataController.doDragEnter(event)" dragDrop="dataController.doDragDrop(event)">
          <mx:columns>
            <mx:Array>
              <mx:DataGridColumn columnName="fName" headerText="First Name" />
              <mx:DataGridColumn columnName="lName" headerText="Last Name" />
              <mx:DataGridColumn columnName="location" headerText="Location" />
            </mx:Array>
          </mx:columns>
        </mx:DataGrid>
      </mx:Panel>
      <mx:Panel id="supPanel" title="Support Group" width="{empPanel.width}">
        <mx:DataGrid dragEnter="dataController.doDragEnter(event)" dragDrop="dataController.doDragDrop(event)">
          <mx:columns>
            <mx:Array>
              <mx:DataGridColumn columnName="fName" headerText="First Name" />
              <mx:DataGridColumn columnName="lName" headerText="Last Name" />
              <mx:DataGridColumn columnName="location" headerText="Location" />
             </mx:Array>
          </mx:columns>
        </mx:DataGrid>
      </mx:Panel>
    </mx:VBox>
  </mx:HBox>
</mx:Application>